package com.jwthhealth_pub.old.diet.view.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.view.NullFragment;
import com.jwthhealth_pub.old.diet.view.fragment.ConstitutionFragment;
import com.jwthhealth_pub.old.diet.view.fragment.NursedFragment;
import com.jwthhealth_pub.old.diet.view.fragment.SeasonsFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * diet adatper
 * Created by Administrator on 2016/12/27.
 */
public class DietAdapter extends FragmentPagerAdapter {

    private Context context;
    private TabLayout tabLayout;

    private List<String> tabTitles;
    //tabview holder
    private List<ViewHolder> holders;

    private List<String> mTagList; // 用来存放所有的 Tag

    public DietAdapter(FragmentManager fm, Context context , TabLayout tabLayout) {
        super(fm);
        this.context = context;
        this.tabLayout = tabLayout;

        tabTitles = new ArrayList<>();
        Resources resources = context.getResources();
        tabTitles.add(resources.getString(R.string.diet_title_one));
        tabTitles.add(resources.getString(R.string.diet_title_two));
        tabTitles.add(resources.getString(R.string.diet_title_thi));

        holders = new ArrayList<>();

        mTagList = new ArrayList<>();
        for (int i=0; i<3; i++){
            mTagList.add(i, null);
        }
    }

    // 生成 Tag
// 直接从 FragmentPageAdapter 源码里拷贝 Fragment 生成 Tag 的方法
    private String makeFragmentName(int viewId, int index) {
        return "android:switcher:" + viewId + ":" + index;
    }

    // 将 Tag 缓存到 List 中
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        mTagList.add(position, makeFragmentName(container.getId(),
//                (int) getItemId(position)));
        return super.instantiateItem(container, position);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new NullFragment();

        switch (position) {
            case 0:
                //九种体制
                fragment = new ConstitutionFragment();
                break;
            case 1:
                //常见病调理
                fragment = new NursedFragment();
                break;
            case 2:
                //四季养生
                fragment = new SeasonsFragment();
                break;
            default: break;
        }

        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    /**
     * 用作自定义选项卡样式
     *
     * @param position
     * @return
     */
    public View getTabView(int position) {
        // TODO: 2016/8/2 activity 以外的地方如何使用butterknife
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_diet_tab, null));
        holders.add(holder);
        holder.tag.setText(tabTitles.get(position));
        holder.tag.setTextColor(context.getResources().getColor(R.color.primarycolor));
        if (position == 0){
            holder.state(true, position);
        } else{
            holder.state(false, position);
        }

        return holder.view;
    }


    /**
     * 用于切换tab icon的选中状态
     * @param isSelected
     * @param tab
     */
    public void changeState(boolean isSelected, TabLayout.Tab tab){

        if (tab != null){
            int pos = tab.getPosition();

            if(isSelected){
                ViewHolder selHolder = holders.get(pos);
                selHolder.state(true, pos);
                tab.setCustomView(selHolder.getView());
            }else{
                ViewHolder unSelHolder = holders.get(pos);
                unSelHolder.state(false, pos);
                tab.setCustomView(unSelHolder.getView());
            }
        }

    }

    private class ViewHolder{

        private View view;

        private RelativeLayout tagLayout;
        private TextView tag;

        public ViewHolder(View view) {
            this.view = view;

            tagLayout = (RelativeLayout) view.findViewById(R.id.item_layout);
            tag = (TextView) view.findViewById(R.id.item_tag);
        }

        public View getView() {
            return view;
        }

        public void state(boolean isSelected, int position){
            Resources resources = context.getResources();
            if (isSelected){
                tag.setTextColor(resources.getColor(R.color.primarycolor));
//                tagLayout.setBackground(resources.getDrawable(R.drawable.btn_reac_bg));
            }else{
                tag.setTextColor(resources.getColor(R.color.textColorBlack));
//                tagLayout.setBackgroundColor(resources.getColor(R.color.white));
            }
        }
    }
}
